package com.shensi.adminCollect.service.station.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.common.core.domain.entity.SysUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.adminCollect.domain.station.CustomListColumn;
import com.shensi.adminCollect.domain.station.dto.CustomListColumnDto;
import com.shensi.adminCollect.domain.station.vo.CustomListColumnVo;
import com.shensi.adminCollect.mapper.station.CustomListColumnMapper;
import com.shensi.adminCollect.service.station.CustomListColumnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CustomListColumnServiceImpl extends ServiceImpl<CustomListColumnMapper, CustomListColumn> implements CustomListColumnService {

    // 默认所有字段
    @Value("${order.orderColumnId}")
    private String orderColumnId;

    @Override
    public Boolean addListColumn(CustomListColumnDto customListColumnDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(null==user){
            throw new RuntimeException("当前用户不存在");
        }
        CustomListColumn customListColumn = BeanUtil.copyProperties(customListColumnDto, CustomListColumn.class);
        LambdaQueryWrapper<CustomListColumn> wrapper = new LambdaQueryWrapper();
        //判断账号是否油企油站或者admin
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            wrapper.eq(CustomListColumn::getEnterpriseId,user.getEnterpriseId());
            customListColumn.setEnterpriseId(user.getEnterpriseId());
        }else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            wrapper.eq(CustomListColumn::getStationId,user.getStationId());
            customListColumn.setStationId(user.getStationId());
        }else {
            wrapper.eq(CustomListColumn::getIsAdmin,1);
            customListColumn.setIsAdmin(1);
        }
        wrapper.eq(CustomListColumn::getTableId,customListColumnDto.getTableId());
        List<CustomListColumn> list = list(wrapper);

        if(!CollectionUtils.isEmpty(list)){
            LambdaQueryWrapper<CustomListColumn> removeWrapper = new LambdaQueryWrapper();
            //判断账号是否油企油站或者admin
            if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
                removeWrapper.eq(CustomListColumn::getEnterpriseId,user.getEnterpriseId());
            }else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
                removeWrapper.eq(CustomListColumn::getStationId,user.getStationId());
            }else {
                removeWrapper.eq(CustomListColumn::getIsAdmin,1);
            }
            removeWrapper.eq(CustomListColumn::getTableId,customListColumnDto.getTableId());
            remove(removeWrapper);
        }
        customListColumn.setCreateDate(new Date());
        customListColumn.setCreateUser(user.getUserId());
        boolean flag = save(customListColumn);
        return flag;
    }

    @Override
    public CustomListColumnVo selectListColumn(Integer tableId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(null==user){
            throw new RuntimeException("当前用户不存在");
        }
        LambdaQueryWrapper<CustomListColumn> wrapper = new LambdaQueryWrapper();
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            wrapper.eq(CustomListColumn::getEnterpriseId,user.getEnterpriseId());
        }else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            wrapper.eq(CustomListColumn::getStationId,user.getStationId());
        }else {
            wrapper.eq(CustomListColumn::getIsAdmin,1);
        }
        wrapper.eq(CustomListColumn::getTableId,tableId);
        CustomListColumn customListColumn = getOne(wrapper);
        CustomListColumnVo vo = new CustomListColumnVo();
        //如果当前没有自定义字段就默认展示全部
        if(null==customListColumn){
            CustomListColumn column = new CustomListColumn();
            if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
                column.setEnterpriseId(user.getEnterpriseId());
            }else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
                column.setStationId(user.getStationId());
            }else {
                column.setIsAdmin(1);
            }
            column.setTableId(tableId);
            column.setColumnId(orderColumnId);
            column.setCreateDate(new Date());
            column.setCreateUser(user.getUserId());
            save(column);
            vo.setColumnId(orderColumnId);
        } else {
            vo.setColumnId(customListColumn.getColumnId());
        }
        return vo;
    }


}
